package com.mes.web.controller.biz;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizProduct;
import com.mes.biz.domain.BizProductInventory;
import com.mes.biz.domain.BizPurchase;
import com.mes.biz.domain.vo.InventoryVo;
import com.mes.biz.domain.vo.PurchaseProductVo;
import com.mes.biz.service.BizProductInventoryService;
import com.mes.biz.service.BizProductService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 产品Controller
 * @author hwg
 * @email hwg
 * @date 2023-04-02
 */
@RestController
@RequestMapping("/biz/bizProductInventory")
public class BizProductInventoryController extends BaseController {

    @Autowired
    private BizProductInventoryService bizProductInventoryService;
    @Resource
    private BizProductService bizProductService;

    /**
     * 查询产品列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(BizProductInventory bizProductInventory, HttpServletRequest request, HttpServletResponse response) {
        bizProductInventory.setPage(new PageDomain(request, response));
        PageInfo<BizProductInventory> page = bizProductInventoryService.findPage(bizProductInventory);
        for (int i = 0; i < page.getList().size(); i++) {
//            if (page.getList().get(i).getAmount() !=null && page.getList().get(i).getAmount().compareTo(BigDecimal.ZERO)>0){
//                page.getList().get(i).setPrice(page.getList().get(i).getAmount().divide(page.getList().get(i).getNumber(),3,BigDecimal.ROUND_HALF_UP));
//            }
            if (page.getList().get(i).getAmount() !=null && page.getList().get(i).getAmount().compareTo(BigDecimal.ZERO)>0 &&
                    page.getList().get(i).getNumber() !=null && page.getList().get(i).getNumber().compareTo(BigDecimal.ZERO)>0){
                page.getList().get(i).setPrice(page.getList().get(i).getAmount().divide(page.getList().get(i).getNumber(),3,BigDecimal.ROUND_HALF_UP));
            }
        }
        for (BizProductInventory productInventory: page.getList()){
            if (productInventory.getNumbers()!=null && productInventory.getNewPrice() != null){
                productInventory.setPrice(productInventory.getNewPrice().divide(new BigDecimal(productInventory.getNumbers()),3,BigDecimal.ROUND_HALF_UP));
            }
        }
        return R.data(page);
    }

    /**
     * 查询产品列表
     */
    @GetMapping("/detailList")
    public R<PageInfo> detailList(BizProductInventory bizProductInventory, HttpServletRequest request, HttpServletResponse response) {
        bizProductInventory.setPage(new PageDomain(request, response));
        return R.data(bizProductInventoryService.findDetailPage(bizProductInventory));
    }

    /**
     * 修改库存台账
     */
    @Log(title = "库存台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizProductInventory bizProductInventory) {
        BigDecimal number=bizProductInventory.getNumber();
        bizProductInventory = bizProductInventoryService.get(bizProductInventory);
        BizProduct bizProduct = bizProductService.getBizProductByProductCode(bizProductInventory.getProductId());
        bizProductInventory.setNumber(number);
        bizProductInventory.setAssistNumberA(bizProductInventory.getNumber().multiply(bizProduct.getConvertRatioA()).setScale(2,BigDecimal.ROUND_HALF_UP));
        bizProductInventory.setAssistNumberB(bizProductInventory.getNumber().multiply(bizProduct.getConvertRatioB()).setScale(2,BigDecimal.ROUND_HALF_UP));
        return R.status(bizProductInventoryService.save(bizProductInventory));
    }

    /**
     * 查询库存明细列表
     */
    @GetMapping("/listInventoryStat")
    public R<PageInfo> listInventoryStat(BizProductInventory bizProductInventory, HttpServletRequest request, HttpServletResponse response) {
        bizProductInventory.setPage(new PageDomain(request, response));
        PageInfo<InventoryVo> page = bizProductInventoryService.listInventoryStatPage(bizProductInventory);
        return R.data(page);
    }

    /**
     * 打印采购单明细
     */
    @GetMapping("/printInventoryStat")
    public void export(BizProductInventory bizProductInventory, HttpServletResponse response) {
        List<InventoryVo> list = bizProductInventoryService.listInventoryStat(bizProductInventory);
        ExcelUtil<InventoryVo> util = new ExcelUtil<>(InventoryVo.class);
        util.printExcel(response,list, "库存明细表");
    }

}
